﻿namespace Pps.Gui
{
    using Microsoft.SPOT;
    using Pps.Gui.Base;
    using Pps.Gui.Lcd;

    public delegate void WindowEvent();

    public abstract class Window : DisplayObjectContainer
    {
        private Control selectedControl;

        public event WindowEvent Activated;
        public event WindowEvent Deactivated;

        public Window()
            : base(Bitmap.MaxWidth, Bitmap.MaxHeight)
        {
            this.selectedControl = null;
        }

        public void AddChildControl(Control child)
        {
            child.Parent = this;

            this.AddChild(child);
        }

        public void RemoveChildControl(Control child)
        {
            this.RemoveChild(child);
        }

        public bool IsActive()
        {
            return WindowManager.ActiveWindow == this;
        }

        internal void ActivateWindow()
        {
            if (this.Activated != null)
            {
                this.Activated();
            }
        }

        internal void DeactivateWindow()
        {
            if (this.selectedControl != null)
            {
                this.selectedControl.OnClickedEvent();
            }

            if (this.Deactivated != null)
            {
                this.Deactivated();
            }
        }

        internal override void Render()
        {
            if (this.IsActive() == true)
            {
                this.RenderToDisplay();
            }
        }

        private void RenderToDisplay()
        {
            Graphics gfx = new Graphics(0, 0);

            gfx.DrawRectangle(this.BackgroundColor, 0, 0, 0, this.Width, this.Height, 0, 0, this.BackgroundColor, 0, 0, this.BackgroundColor, 0, 0, 256);

            this.Paint(gfx);

            for (int i = 0; i < this.Children.Count; i++)
            {
                Control c = this.Children[i] as Control;

                if (c != null)
                {
                    if (c.Visible == true)
                    {
                        c.Render();
                    }
                    else
                    {
                        gfx.DrawRectangle(this.BackgroundColor, 0, this.X, this.Y, this.Width, this.Height, 0, 0, this.BackgroundColor, 0, 0, this.BackgroundColor, 0, 0, 256);
                    }
                }
            }

            gfx.Flush();
        }

        internal void TouchDown(Touch touch)
        {
            Control c = this.GetDisplayObjectAt(touch) as Control;

            if (c != null && c.Enabled == true)
            {
                this.selectedControl = c;
                c.OnTouchedEvent();
            }
        }

        internal void TouchUp(Touch touch)
        {
            Control c = this.GetDisplayObjectAt(touch) as Control;

            if (c != null && c.Enabled == true)
            {
                if (this.selectedControl == c)
                {
                    c.OnClickedEvent();
                }

                if (this.selectedControl != null && this.selectedControl != c)
                {
                    this.selectedControl.OnLostFocusEvent();
                }
            }

            this.selectedControl = null;
        }
    }
}